//-----------------------------------------------------------------------
//<copyright file="PhieuNhapDAO.cs
//Code generate by tools.
//</copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using PMS.Exceptions;
using System.Data.SqlClient;
using PMS.Entity;

namespace PMS.DAL
{
	public class PhieuNhapDAO : SQLDatabase, IPhieuNhapDAO
	{

		#region Create PhieuNhap methods

		public PhieuNhap CreatePhieuNhap(PhieuNhap phieuNhap)
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuNhapInsert");
				AddOutParameter(command, "@SoPN", SqlDbType.NVarChar, phieuNhap.SoPN);
				AddInParameter(command, "@NgayNhapKho", SqlDbType.DateTime, phieuNhap.NgayNhapKho);
				AddInParameter(command, "@NguoiNhap", SqlDbType.NVarChar, phieuNhap.NguoiNhap);
				AddInParameter(command, "@NgayLapPhieu", SqlDbType.DateTime, phieuNhap.NgayLapPhieu);
				AddInParameter(command, "@NguoiLap", SqlDbType.NVarChar, phieuNhap.NguoiLap);
				AddInParameter(command, "@SoHD", SqlDbType.NVarChar, phieuNhap.SoHD);
				AddInParameter(command, "@LyDoNhap", SqlDbType.NVarChar, phieuNhap.LyDoNhap);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, phieuNhap.GhiChu);
				AddInParameter(command, "@MaNCC", SqlDbType.NVarChar, phieuNhap.MaNCC);
				AddInParameter(command, "@MaKho", SqlDbType.NVarChar, phieuNhap.MaKho);

				ExecuteNonQuery(command);
                phieuNhap.SoPN = (string)GetParameterValue(command, "@SoPN");
				return phieuNhap;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access CreatePhieuNhapException", ex);
			}
		}

		#endregion

		#region Update PhieuNhap methods

		public void UpdatePhieuNhap(PhieuNhap phieuNhap)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuNhapUpdate");

				AddInParameter(command, "@SoPN", SqlDbType.NVarChar, phieuNhap.SoPN);
				AddInParameter(command, "@NgayNhapKho", SqlDbType.DateTime, phieuNhap.NgayNhapKho);
				AddInParameter(command, "@NguoiNhap", SqlDbType.NVarChar, phieuNhap.NguoiNhap);
				AddInParameter(command, "@NgayLapPhieu", SqlDbType.DateTime, phieuNhap.NgayLapPhieu);
				AddInParameter(command, "@NguoiLap", SqlDbType.NVarChar, phieuNhap.NguoiLap);
				AddInParameter(command, "@SoHD", SqlDbType.NVarChar, phieuNhap.SoHD);
				AddInParameter(command, "@LyDoNhap", SqlDbType.NVarChar, phieuNhap.LyDoNhap);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, phieuNhap.GhiChu);
				AddInParameter(command, "@MaNCC", SqlDbType.NVarChar, phieuNhap.MaNCC);
				AddInParameter(command, "@MaKho", SqlDbType.NVarChar, phieuNhap.MaKho);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access UpdatePhieuNhapException", ex);
			}
		}

		#endregion

		#region Delete PhieuNhap methods

		public void DeletePhieuNhap(PhieuNhap phieuNhap)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuNhapDelete");

				AddInParameter(command, "@SoPN", SqlDbType.NVarChar, phieuNhap.SoPN);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access DeletePhieuNhapException", ex);
			}
		}

		#endregion

		#region Select methods

		#region GetAll PhieuNhap methods

		public PhieuNhaps GetAllPhieuNhap()
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuNhapGetList");
				PhieuNhaps Result = new PhieuNhaps();
				List<PhieuNhap> List = ExecuteQuery<PhieuNhap>(command,true);
				foreach(PhieuNhap item in List)
				{
					Result.Add(item);
				}
				return Result;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetAllPhieuNhapException", ex);
			}
		}

		#endregion


		#region GetPhieuNhap methods

		public PhieuNhap GetPhieuNhapByID(string soPN)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spPhieuNhapGet");

				AddInParameter(command, "@soPN", SqlDbType.NVarChar, soPN);

				return ExecuteQuery<PhieuNhap>(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetPhieuNhapBy ID Exception", ex);
			}
		}

		#endregion

        #region GetPhieuNhapByDate methods

        public PhieuNhaps GetPhieuNhapByDate(DateTime? tungay, DateTime? denngay)
        {
            try
            {
                SqlCommand command = SQLDatabase.StoredProcedureCommand("spGetPhieuNhapListByDate");

                AddInParameter(command, "@tungay", SqlDbType.DateTime, tungay);
                AddInParameter(command, "@denngay", SqlDbType.DateTime, denngay);

                PhieuNhaps Result = new PhieuNhaps();
                List<PhieuNhap> List = ExecuteQuery<PhieuNhap>(command, true);
                foreach (PhieuNhap item in List)
                {
                    Result.Add(item);
                }
                return Result;
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access GetPhieuNhapBy ID Exception", ex);
            }
        }

        #endregion

                
        
		#endregion
	}
}//End Class
